package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.context.BaseContext;
import org.example.json.jwtUserId;
import org.example.pojo.Result;
import org.example.pojo.User;
import org.example.properties.JwtProperties;
import org.example.service.UserService;
import org.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private UserService userService;

//    @GetMapping("hello")
//    public Result<String> hello() {
//        return Result.success("hello");
//    }

//    @GetMapping("/login")
//    public Result<String> getTokenByLogin1(String account,String password) {
//        log.info("用户"+account+"登录");
//        User user=new User();
//        user.setAccount(account);
//        user.setPassword(password);
//        user=userService.getUserByLogin(user);
//        if(user!=null) {
//            Map<String, Object> claims=new HashMap<>();
//            claims.put("id",user.getId());
//            claims.put("account",user.getAccount());
//            claims.put("username",user.getUsername());
//
//            String jwt = JwtUtils.generateJwt(claims);
//            return Result.success(jwt);
//        }
//        return Result.error("用户名或密码错误！");
//    }

    @PostMapping("/login")
    public Result<jwtUserId> getTokenByLogin2(@RequestBody User user) {
        log.info("账号" + user.getAccount() + "登录");
        user = userService.getUser(user);
        if (user != null) {
            Map<String, Object> claims = new HashMap<>();
//            Long id=user.getId();
            claims.put("id", user.getId());
            claims.put("account", user.getAccount());
            claims.put("username", user.getUsername());
            log.info( "登录token   "+user.getId()+"   "+ user.getAccount()+"    "+user.getUsername());
            String jwt = JwtUtils.generateJwt(claims,jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl());
            String userId= String.valueOf(user.getId());
            jwtUserId jwtUserId = new jwtUserId();
            jwtUserId.setJwt(jwt);
            jwtUserId.setUserId(userId);
//            log.info("jwt是:"+jwt);
//            log.info("jwt:{}",jwt);
            return Result.success(jwtUserId);
        }
        return Result.error("用户名或密码错误！");
    }

    @PostMapping("/register")
    public Result<String> register(@RequestBody User user) {
        log.info("未知账号" + user.getAccount() + "注册");

        User pre=User.builder()
                .account(user.getAccount())
                .build();
        User flag=userService.getUser(pre); //只用账号进行搜索，确保账号的唯一性
        if(flag==null) {
            user.setAvatar("/static/Logo.png");
            userService.setUser(user); //注册时只需要account,password,username,avatar
            return Result.success();
        }
        return Result.error("账号已存在");
    }

    @GetMapping
    public Result<User> getUserById() {
        Long id = BaseContext.getCurrentId();
//        Long id = Long.valueOf(1);
        log.info("获取uid为"+id+"的用户信息");
        User user = User.builder()
                .id(id)
                .build();

        return Result.success(userService.getUser(user));
    }
}
